package com.controller;

import com.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class Main {

    @Autowired
    UserService service;


    @GetMapping("/")
    public String main() {
        try {
            service.aaa();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "main";
    }

    @GetMapping("/add")
    public String add() {
        System.out.println("add");
        return "add";
    }

    @GetMapping("/login")
    public String login(ShiroHttpServletRequest httpServletRequest) {

        httpServletRequest.getSession().getId();
        Subject subject = SecurityUtils.getSubject();
        var session = subject.getSession();
// 获取session中所有的键值
        List<Object> attrs = (List<Object>) session.getAttributeKeys();

        for (var a : attrs) {
            System.out.println(a);
            Object obj = session.getAttribute(a);

            System.out.println(obj);
        }


        System.out.println();
        return "login";
    }

    @GetMapping("/update")
    public String update() {
        System.out.println("update");
        return "update";
    }

    @PostMapping("/tologin")
    public String tologin(String usr, String pwd) {

        var subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(usr, pwd);

        try {
            subject.login(token);
            return "redirect:/";
        } catch (UnknownAccountException e) {
            System.out.println("用户名不存在");
            return "redirect:login";
        } catch (IncorrectCredentialsException e) {
            System.out.println("密码错误");
            return "redirect:login";
        }
    }

    @RequestMapping("/unAuth")
    public String unAuth() {
        return "unAuth";
    }


    /**
     * 退出测试，退出后没有任何权限
     */
    @ResponseBody
    @RequestMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        System.out.println(subject == null);
        System.out.println(subject.isRemembered());
        subject.logout();
        // 把以前所创建的session销毁，然后再去创建一个全新的session
        //shrio 的session是不依赖于浏览器的session
        System.out.println(subject.getSession().getId()
        );
        return "success";
    }

//
//    GsonUtils gsonUtils;
//
//    UserService userService;
//
//    @Autowired
//    public void setUserService(UserService userService) {
//        this.userService = userService;
//    }
//
//
//    @Autowired
//    public void setGsonUtils(GsonUtils gsonUtils) {
//        this.gsonUtils = gsonUtils;
//    }
//
//    @GetMapping("/")
//    public String getstr() {
//
//        System.out.println(this.getClass().getName());
//
//        System.out.println("++++++++++++++++++++++++++++");
//        System.out.println(userService.aaa());
//        System.out.println(mapper.getmapper());
//        return "success";
//    }
}
